package com.zhonghui.scm.service.impl;

import com.zhonghui.common.utils.DateUtils;
import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.scm.domain.ScmSaleContract;
import com.zhonghui.scm.domain.ScmSaleContractItem;
import com.zhonghui.scm.mapper.ScmSaleContractMapper;
import com.zhonghui.scm.service.IScmSaleContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 销售合同Service业务层处理
 * 
 * @author zhonghui
 * @date 2022-05-26
 */
@Service
public class ScmSaleContractServiceImpl implements IScmSaleContractService
{
    @Autowired
    private ScmSaleContractMapper scmSaleContractMapper;

    /**
     * 查询销售合同
     * 
     * @param id 销售合同主键
     * @return 销售合同
     */
    @Override
    public ScmSaleContract getById(Long id)
    {
        return scmSaleContractMapper.getById(id);
    }

    /**
     * 查询销售合同列表
     * 
     * @param scmSaleContract 销售合同
     * @return 销售合同
     */
    @Override
    public List<ScmSaleContract> getList(ScmSaleContract scmSaleContract)
    {
        return scmSaleContractMapper.getList(scmSaleContract);
    }

    /**
     * 新增销售合同
     * 
     * @param scmSaleContract 销售合同
     * @return 结果
     */
    @Override
    public int add(ScmSaleContract scmSaleContract)
    {
        scmSaleContract.setContractNumber(generateNumber("XSJH"));
        scmSaleContract.setApplyId(SecurityUtils.getUserId());
        scmSaleContract.setApplyDate(DateUtils.getNowDate());
        scmSaleContract.setSalesManId(SecurityUtils.getUserId());
        int row = scmSaleContractMapper.add(scmSaleContract);
        insertItem(scmSaleContract);
        return row;
    }

    /**
     * 修改销售合同
     * 
     * @param scmSaleContract 销售合同
     * @return 结果
     */
    @Override
    public int edit(ScmSaleContract scmSaleContract)
    {
        if (scmSaleContract.getAuditStatus() != null){
            scmSaleContract.setAuditorId(SecurityUtils.getUserId());
            scmSaleContract.setAuditTime(new Date());
            scmSaleContract.setApplyStatus(2);
        }
        scmSaleContractMapper.delItem(scmSaleContract.getId());
        insertItem(scmSaleContract);
        return scmSaleContractMapper.edit(scmSaleContract);
    }

    /**
     * 批量删除销售合同
     * 
     * @param ids 需要删除的销售合同主键
     * @return 结果
     */
    @Override
    public int del(Long[] ids)
    {
        for (Long id : ids) {
            scmSaleContractMapper.delItem(id);
        }
        return scmSaleContractMapper.del(ids);
    }


    public void insertItem(ScmSaleContract scmSaleContract){
        List<ScmSaleContractItem> scmSaleContractItemList = scmSaleContract.getScmSaleContractItemList();

        if (!scmSaleContractItemList.isEmpty()){

        for (ScmSaleContractItem scmSaleContractItem : scmSaleContractItemList) {
            scmSaleContractItem.setContractId(scmSaleContract.getId());
        }
        scmSaleContractMapper.addItem(scmSaleContractItemList);

        }
    }

    public String generateNumber(String number){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return number+format.format(date);
    }
}
